package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.content.Context;
import android.database.Cursor;

import com.moleq.db.DatabaseHelper;
import com.moleq.models.Scale;

public class ScaleService {
	private DatabaseHelper DBHelper;
	public ScaleService(Context context){
		this.DBHelper = new DatabaseHelper(context);
	}
	public List<Scale> getAllScales(){
		String sql = "select * from t_scale_setting";
		Cursor cursor = DBHelper.rawQuery(sql,null);
		List<Scale> Source = new ArrayList<Scale>();
		if(cursor == null){
			return Source;
		}
		while(cursor.moveToNext()){
			Scale scale = new Scale(
					cursor.getInt(0),
					cursor.getInt(1),
					cursor.getString(2),
					cursor.getString(3),
					cursor.getString(4),
					cursor.getInt(5),
					cursor.getInt(6),
					cursor.getString(7),
					cursor.getString(8),
					cursor.getString(9),
					cursor.getInt(10),
					cursor.getString(11),
					cursor.getInt(12),
					cursor.getString(13)
					);
			Source.add(scale);
		}
		cursor.close();
		return Source;
	}
	
	public String updateScalesItem(Scale scale){
		String sql = "update t_scale_setting set ";
		sql += "sl_port = " + scale.getPort() + ",";
		
		if(scale.getDeviceName() != null && !scale.getDeviceName().equals("")){
			sql += "sl_device_name = '" + scale.getDeviceName() + "',";
		}else{
			sql += "sl_device_name = " + null + ",";
		}
		if(scale.getBaud() != null && !scale.getBaud().equals("")){
			sql += "sl_baud = '" + scale.getBaud() + "',";
		}else{
			sql += "sl_baud = " + null + ",";
		}
		if(scale.getParity() != null && !scale.getParity().equals("")){
			sql += "sl_parity = '" + scale.getParity() + "',";
		}else{
			sql += "sl_parity = " + null + ",";
		}
		if(scale.getDataBits() >= 0){
			sql += "sl_data_bits = " + scale.getDataBits() + ",";
		}else{
			sql += "sl_data_bits = " + null + ",";
		}
		if(scale.getStopBits() >= 0){
			sql += "sl_stop_bits = " + scale.getStopBits() + ",";
		}else{
			sql += "sl_stop_bits = " + null + ",";
		}
		if(scale.getWeightCmd() != null && !scale.getWeightCmd().equals("")){
			sql += "sl_weight_cmd = '" + scale.getWeightCmd() + "',";
		}else{
			sql += "sl_weight_cmd = " + null + ",";
		}
		if(scale.getCancelCmd() != null && !scale.getCancelCmd().equals("")){
			sql += "sl_cancel_cmd = '" + scale.getCancelCmd() + "',";
		}else{
			sql += "sl_cancel_cmd = " + null + ",";
		}
		if(scale.getEndChar() != null && !scale.getEndChar().equals("")){
			sql += "sl_end_char = '" + scale.getEndChar() + "',";
		}else{
			sql += "sl_end_char = " + null + ",";
		}
		if(scale.getTimeout() >= 0){
			sql += "sl_timeout = " + scale.getTimeout() + ",";
		}else{
			sql += "sl_timeout = " + null + ",";
		}
		if(scale.getDataFormat() != null && !scale.getDataFormat().equals("")){
			sql += "sl_data_format = '" + scale.getDataFormat() + "',";
		}else{
			sql += "sl_data_format = " + null + ",";
		}
		if(scale.getDivisor() >= 0 ){
			sql += "sl_divisor = " + scale.getDivisor() + ",";
		}else{
			sql += "sl_divisor = " + null + ",";
		}
		if(scale.getUsing() != null && !scale.getUsing().equals("")){
			sql += "sl_using = '" + scale.getUsing() + "'";
		}else{
			sql += "sl_using = " + null;
		}
		sql += " where sl_no = " + scale.getNo();
		String errorString = DBHelper.execSQL(sql);
		return errorString;
	}
	
	public void deleteItem(int no){
		String sql = "Delete from t_scale_setting where sl_no = " + no;
		DBHelper.execSQL(sql);
	}
	
	public Scale FindScaleByNo(int no){
		String sql = "select * from t_scale_setting where sl_no = " + no;
		Cursor cursor = DBHelper.rawQuery(sql, null);
		Scale scale = null;
		if(cursor == null){
			return scale;
		}
		if(cursor.moveToNext()){
			scale = new Scale(
					cursor.getInt(0),
					cursor.getInt(1),
					cursor.getString(2),
					cursor.getString(3),
					cursor.getString(4),
					cursor.getInt(5),
					cursor.getInt(6),
					cursor.getString(7),
					cursor.getString(8),
					cursor.getString(9),
					cursor.getInt(10),
					cursor.getString(11),
					cursor.getInt(12),
					cursor.getString(13),
					cursor.getString(14),
					cursor.getString(15)
					);
		}
		cursor.close();
		return scale;
	}
	
	public int getMaxNo(){
		String sql = "select Max(sl_no) from t_scale_setting";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		int no = 0;
		if(cursor == null){
			return no;
		}
		while(cursor.moveToNext()){
			no = cursor.getInt(0);
		}
		cursor.close();
		return no;
	}
	
	public String insertItem(Scale scale){
		String sql = "Insert into t_scale_setting values(";
		sql += scale.getNo() + ",";
		sql += scale.getPort() + ",";
		
		if(scale.getDeviceName() != null && !scale.getDeviceName().equals("")){
			sql += "'" + scale.getDeviceName() + "',";
		}else{
			sql += null + ",";
		}
		if(scale.getBaud() != null && !scale.getBaud().equals("")){
			sql += "'" + scale.getBaud() + "',";
		}else{
			sql += null + ",";
		}
		if(scale.getParity() != null && !scale.getParity().equals("")){
			sql += "'" + scale.getParity() + "',";
		}else{
			sql += null + ",";
		}
		if(scale.getDataBits() >= 0){
			sql += scale.getDataBits() + ",";
		}else{
			sql += null + ",";
		}
		if(scale.getStopBits() >= 0){
			sql += scale.getStopBits() + ",";
		}else{
			sql += null + ",";
		}
		if(scale.getWeightCmd() != null && !scale.getWeightCmd().equals("")){
			sql += "'" + scale.getWeightCmd() + "',";
		}else{
			sql += null + ",";
		}
		if(scale.getCancelCmd() != null && !scale.getCancelCmd().equals("")){
			sql += "'" + scale.getCancelCmd() + "',";
		}else{
			sql += null + ",";
		}
		if(scale.getEndChar() != null && !scale.getEndChar().equals("")){
			sql += "'" + scale.getEndChar() + "',";
		}else{
			sql += null + ",";
		}
		if(scale.getTimeout() >= 0){
			sql += scale.getTimeout() + ",";
		}else{
			sql += null + ",";
		}
		if(scale.getDataFormat() != null && !scale.getDataFormat().equals("")){
			sql += "'" + scale.getDataFormat() + "',";
		}else{
			sql += null + ",";
		}
		if(scale.getDivisor() >= 0 ){
			sql += scale.getDivisor() + ",";
		}else{
			sql += null + ",";
		}
		if(scale.getUsing() != null && !scale.getUsing().equals("")){
			sql += "'" + scale.getUsing() + "',";
		}else{
			sql += null + ",";
		}
		sql += null + "," + null + ")";
		String errorString = DBHelper.execSQL(sql);
		return errorString;
	}
	
}
